#include "usersdb.h"

UsersDB::UsersDB(QObject *parent) :
    QObject(parent)
{
}

/**
 * @brief 获取所有玩家姓名
 *
 * @return QList<QString> 玩家姓名列表
 */
QList<QString> UsersDB::getUsersName()
{
    QList<QString> usersNameList;
    // 使用QSqlQuery查询连接整张表，先要使用连接名获取该连接
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec("select name from users order by addTime desc");
    while(query.next())
    {
        usersNameList.append(query.value(0).toString());
    }
    return usersNameList;
}

/**
 * @brief 获取最大分数
 *
 * @return QString
 */
QString UsersDB::getUsersMaxScore(const QString &username)
{
    QString score;
    // 使用QSqlQuery查询连接整张表，先要使用连接名获取该连接
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("select maxscore from users where name = '%1'")
                           .arg(username));
    while(query.next())
    {
        score = query.value(0).toString();
    }
    return score;
}

/**
 * @brief 获取游戏中玩家的所有信息
 *
 * @return UserModel 玩家所以信息
 */
UserModel UsersDB::getUserInfo()
{
    UserModel user;
    // 使用QSqlQuery查询连接整张表，先要使用连接名获取该连接
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("select * from users where isUsing = 1"));
    while(query.next())
    {
        user.name = query.value(0).toString();
        user.fontsize = query.value(1).toInt();
        user.difficulty = query.value(2).toInt();
        user.touch = query.value(3).toInt();
        user.maxscore = query.value(4).toInt();
        user.isUsing = query.value(5).toInt();
        user.addTime = query.value(6).toString();
    }
    return user;
}

/**
 * @brief 创建玩家和初始化参数
 *
 * @param username 玩家姓名
 */
void UsersDB::creatUserInfo(const QString &username)
{
    // 使用QSqlQuery查询连接整张表，先要使用连接名获取该连接
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("insert into users values( '%1', 24, 1, 1, 0, 0, "
                       " datetime('now', 'localtime'))").arg(username));
}

/**
 * @brief 更新玩家进行游戏状态
 *
 * @param username 玩家姓名
 */
void UsersDB::updateIsUsing(const QString &username)
{
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("update users set isUsing = 0"));
    query.exec(QString("update users set isUsing = 1, addTime = datetime('now', 'localtime') "
                       "where name = '%1'").arg(username));
}

/**
 * @brief 更新参数信息
 *
 * @param user 用户所以信息
 */
void UsersDB::updateParam(const UserModel &user)
{
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("update users set fontSize = %1, difficulty = %2, touch = %3 "
                       "where name = '%4'").arg(user.fontsize)
               .arg(user.difficulty).arg(user.touch).arg(user.name));
}

/**
 * @brief 更新最大分数
 *
 * @param score
 */
void UsersDB::updateMaxScore(const QString &score)
{
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("update users set maxscore = %1 where isUsing = 1")
               .arg(score.toInt()));
}

/**
 * @brief 判断玩家姓名是否重复
 *
 * @param username 姓名
 * @return bool 重复返回假;否则返回真
 */
bool UsersDB::isUserRepeat(const QString &username)
{
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("select * from users where name = '%1'").arg(username));
    while (query.next())
    {
        if ( query.ValuesAsColumns)
            return false;
    }
    return true;
}

/**
 * @brief 获取玩家字体大小设置
 *
 * @param username 玩家姓名
 * @return int 字体大小
 */
int UsersDB::getFontSize(const QString &username)
{
    QSqlDatabase db = QSqlDatabase::database("my.db");
    QSqlQuery query(db);
    query.exec(QString("select fontSize from users where name = '%1'").arg(username));
    while (query.next())
    {
        return query.value(0).toInt();
    }
    return 0;
}
